package com.jy.datapipeline.importserver.handler;

import com.jy.datapipeline.common.util.BeanFactoryUtil;
import com.jy.datapipeline.common.constants.PrefixConstants;
import com.jy.datapipeline.importserver.common.constants.KeyConstant;
import com.jy.datapipeline.importserver.common.enums.impl.RetrySubtaskEnum;
import com.jy.datapipeline.common.util.SnowFlakeUtil;
import com.jy.datapipeline.importserver.entity.ImportRetrySubtask;
import com.jy.datapipeline.importserver.entity.ImportSubtask;
import com.jy.datapipeline.importserver.entity.manager.EntityManager;
import com.jy.datapipeline.importserver.service.ImportRetrySubtaskService;
import com.jy.datapipeline.importserver.service.ImportSubTaskService;
import com.jy.datapipeline.importserver.support.QuartzSupport;
import lombok.SneakyThrows;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;

import javax.annotation.PostConstruct;
import java.util.Date;

/**
 * @类名 ResendSubtaskHandler
 * @描述 TODO
 * @作者 白条君
 * @创建日期 2023/6/25 23:14
 * @版本 1.0
 */
public class ScheduleRetrySubtaskHandler extends QuartzJobBean {

    private final ImportRetrySubtaskService importRetrySubtaskService;

    private final ImportSubTaskService importSubTaskService;

    private final QuartzSupport quartzSupport;

    public ScheduleRetrySubtaskHandler() {
        importRetrySubtaskService = BeanFactoryUtil.getBean(ImportRetrySubtaskService.class);
        importSubTaskService = BeanFactoryUtil.getBean(ImportSubTaskService.class);
        quartzSupport = BeanFactoryUtil.getBean(QuartzSupport.class);
    }

    @SneakyThrows
    @Override
    protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
        JobDataMap jobDataMap = context.getJobDetail().getJobDataMap();
        Long id = (Long) jobDataMap.get("id");
        Integer trySubtaskLoadCount = (Integer) jobDataMap.get("trySubtaskLoadCount");

        //查询当前执行重试次数
        Integer retryLoadCurrentCount = importSubTaskService.getRetryLoadCurrentCount(id);
        if (retryLoadCurrentCount != null) {
            //更新子任务执行次数
            ImportSubtask importSubtask = new ImportSubtask();
            importSubtask.setId(id);
            importSubtask.setUpdatetime(new Date());
            importSubtask.setTrySubtaskLoadCurrentCount(retryLoadCurrentCount + 1);
            importSubTaskService.modifySubTaskRetryLoadCount(importSubtask);
        }

        //新增重试子任务
        ImportRetrySubtask importRetrySubtask = (ImportRetrySubtask) EntityManager.create(KeyConstant.IMPORT_RETRY_SUBTASK_KEY);
        importRetrySubtask.setRetrySubtaskId(PrefixConstants.RETRY_SUBTASK_PREFIX + "_" + SnowFlakeUtil.getNextId());
        importRetrySubtask.setSubtaskPrimaryKeyId(id);
        importRetrySubtask.setStatus(String.valueOf(RetrySubtaskEnum.UN_EXECUTION.getCode()));
        importRetrySubtask.setCreateTime(new Date());
        importRetrySubtaskService.add(importRetrySubtask);

        //删除定时任务
        if ((trySubtaskLoadCount - 1) == retryLoadCurrentCount) {
            quartzSupport.delete(PrefixConstants.SUBTASK_PREFIX + "_" + id, "import_subtask");
        }
    }
}
